package cn.moningxi.controller;

import cn.moningxi.bean.CConsignee;
import cn.moningxi.bean.CustomerInfomation;
import cn.moningxi.bean.OrderMaster;
import cn.moningxi.service.CBalanceLogService;
import cn.moningxi.service.CConsigneeService;
import cn.moningxi.service.CustomerInfomationService;
import cn.moningxi.service.OrderMasterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@SessionAttributes(value = "onlineUser", types = String.class)
@Controller
public class OrderMasterController {
    @Autowired
    private OrderMasterService orderMasterService;
    @Autowired
    private CustomerInfomationService customerInfomationService;
    @Autowired
    private CBalanceLogService cBalanceLogService;
    @Autowired
    private CConsigneeService cConsigneeService;

    /**
     * 订单管理获取订单数据
     *
     * @param mv
     * @param request
     * @return
     */
    @RequestMapping("/cntApi/orderManager")
    public ModelAndView getOrder(ModelAndView mv, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        List<OrderMaster> orderMasterList = orderMasterService.getOrder(onlineUser.getCustomerId());
        mv.addObject("orderManager", orderMasterList);
        mv.setViewName("business/person/order");
        return mv;
    }

    /**
     * 查询全部订单信息
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping("/orderInfomation")
    public ModelAndView getAllOrderInfomation(ModelAndView modelAndView) {
        List<OrderMaster> orderList = orderMasterService.selectAllOrderInfomation();
        modelAndView.addObject("orderList", orderList);
        modelAndView.setViewName("backstage/order-list");
        return modelAndView;
    }

    /**
     * 根据订单id查询订单
     *
     * @param modelAndView
     * @param orderId
     * @return
     */
    @RequestMapping("/selectOrderInfoById/{orderId}")
    public ModelAndView selectOrderInfoById(ModelAndView modelAndView, @PathVariable("orderId") Integer orderId) {
        OrderMaster orderDetails = orderMasterService.selectOrderInfoById(orderId);
        modelAndView.addObject("orderDetails", orderDetails);
        modelAndView.setViewName("backstage/order-details");
        return modelAndView;
    }

    /**
     * 结算所有订单
     *
     * @param mv
     * @param orderMoney
     * @param request
     * @return
     */
    @RequestMapping("orderApi/unifiedOrder")
    public ModelAndView unifiedOrder(ModelAndView mv, Integer orderMoney, HttpServletRequest request) {
        CustomerInfomation onlineUser = (CustomerInfomation) request.getSession().getAttribute("onlineUser");
        onlineUser = customerInfomationService.queryUser(onlineUser);
        onlineUser.setCustomerBalance(onlineUser.getCustomerBalance() - orderMoney);
        //更新用户余额
        //结算所有订单
        orderMasterService.unifiedOrder(onlineUser);
        //更新余额日志
        cBalanceLogService.insertByOrder(orderMoney, onlineUser.getCustomerId());
        //重新查询用户信息
        CustomerInfomation user = customerInfomationService.queryUserById(onlineUser.getCustomerInfoId());
        mv.addObject("onlineUser", user);
        mv.addObject("orderMoney", orderMoney);
        //查询地址
        List<CConsignee> consigneeList = cConsigneeService.addrShow(onlineUser);
        mv.addObject("consigneeList", consigneeList);
        mv.setViewName("business/home/success");
        return mv;
    }


    /**
     * 支付页面删除订单
     *
     * @param mv
     * @param orderId
     * @return
     */
    @RequestMapping("orderApi/deleteOrder")
    public ModelAndView deleteOrder(ModelAndView mv, Long orderId) {
        orderMasterService.deleteOrderById(orderId);
        mv.setViewName("redirect:/orderApi/queryOrderByCId");
        return mv;
    }
}
